﻿// <copyright file="DepartmentService.cs" company="neozhu/MVC5-Scaffolder">
// Copyright (c) 2017 All Rights Reserved
// </copyright>
// <author>neo.zhu</author>
// <date>9/27/2017 10:04:42 AM </date>
// <summary>
//  实现定义的业务逻辑,通过依赖注入降低模块之间的耦合度
//   
//  
//  
// </summary>
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using Repository.Pattern.Repositories;
using Service.Pattern;
using WebApp.Models;
using WebApp.Repositories;
using System.Data;
using System.Reflection;
using Newtonsoft.Json;
using Repository.Pattern.Infrastructure;
using System.IO;
using System.Threading.Tasks;

namespace WebApp.Services
{
  public class DepartmentService : Service<Department>, IDepartmentService
  {

    private readonly IRepositoryAsync<Department> _repository;
    private readonly IDataTableImportMappingService _mappingservice;
    public DepartmentService(IRepositoryAsync<Department> repository, IDataTableImportMappingService mappingservice)
        : base(repository)
    {
      _repository = repository;
      _mappingservice = mappingservice;
    }

    public IEnumerable<Department> GetByCompanyId(int companyid)
    {
      return _repository.GetByCompanyId(companyid);
    }



    public async Task ImportDataTableAsync(System.Data.DataTable datatable)
    {
      var mapping = await _mappingservice.Queryable().Where(x => x.EntitySetName == "Department" && x.IsEnabled == true).ToListAsync();
      foreach (DataRow row in datatable.Rows)
      {

        var item = new Department();
       

        foreach (var field in mapping)
        {

          var defval = field.DefaultValue;
          var contation = datatable.Columns.Contains(( field.SourceFieldName == null ? "" : field.SourceFieldName ));
          if (contation && row[field.SourceFieldName] != DBNull.Value)
          {
            Type departmenttype = item.GetType();
            PropertyInfo propertyInfo = departmenttype.GetProperty(field.FieldName);
            propertyInfo.SetValue(item, Convert.ChangeType(row[field.SourceFieldName], propertyInfo.PropertyType), null);
          }
          else if (!string.IsNullOrEmpty(defval))
          {
            Type departmenttype = item.GetType();
            PropertyInfo propertyInfo = departmenttype.GetProperty(field.FieldName);
            if (defval.ToLower() == "now" && propertyInfo.PropertyType == typeof(DateTime))
            {
              propertyInfo.SetValue(item, Convert.ChangeType(DateTime.Now, propertyInfo.PropertyType), null);
            }
            else
            {
              propertyInfo.SetValue(item, Convert.ChangeType(defval, propertyInfo.PropertyType), null);
            }
          }
        }

        this.Insert(item);


      }
    }

    public async Task<Stream> ExportExcelAsync(string filterRules = "", string sort = "Id", string order = "asc")
    {
      var filters = JsonConvert.DeserializeObject<IEnumerable<filterRule>>(filterRules);

      var departments = await this.Query(new DepartmentQuery().Withfilter(filters)).Include(p => p.Company).OrderBy(n => n.OrderBy(sort, order)).SelectAsync();

      var datarows = departments.Select(n => new { CompanyName = ( n.Company == null ? "" : n.Company.Name ), Id = n.Id, Name = n.Name, Manager = n.Manager, CompanyId = n.CompanyId }).ToList();

      return await ExcelHelper.ExportExcelAsync(typeof(Department), datarows);

    }
  }
}



